package com.hpxx.ylzswl.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.hpxx.ylzswl.MainActivity;
import com.hpxx.ylzswl.R;
import com.universal.frame.generalutils.Constants;
import com.universal.frame.generalutils.SharedPreferencesUtil;

import com.hpxx.ylzswl.activity.LoginActivity;
import com.hpxx.ylzswl.base.BaseFragment;
import com.hpxx.ylzswl.utils.ConstantsUtils;

/**
 * @author RoreyDiu
 * @TitleName 引导页
 */
public class LeadingFragment extends BaseFragment {

    private ImageView ivGuideImg;
    private Button btnGuide;
    private int currentPager = -1;
    private Activity activity;

    public static LeadingFragment newInstance(int currentPager) {
        LeadingFragment mGuideFragment = new LeadingFragment();
        Bundle args = new Bundle();
        args.putInt(Constants.ACTIVITY_PUTEXTRA_CURRENT_PAGER, currentPager);
        mGuideFragment.setArguments(args);
        return mGuideFragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.currentPager = getArguments().getInt(Constants.ACTIVITY_PUTEXTRA_CURRENT_PAGER);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_guide;
    }

    @Override
    public void initView(View view) {
        ivGuideImg = (ImageView) view.findViewById(R.id.iv_guide_img);
        btnGuide = (Button) view.findViewById(R.id.btn_guide);
        //===================================================
        btnGuide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(SharedPreferencesUtil.getBoolean(getActivity(), ConstantsUtils.IS_LOGIN)){
                    activity.startActivity(new Intent(getActivity(), MainActivity.class));
                    activity.finish();
                }else{
                    activity.startActivity(new Intent(getActivity(), LoginActivity.class));
                    activity.finish();
//                    com.hpxx.ylzswl.activity.startActivity(new Intent(getActivity(), MainActivity.class));
                }
            }
        });
        //===================================================
        if (currentPager != -1) {
            this.ivGuideImg.setBackgroundResource(currentPager);
        }
    }

    @Override
    public void onFragmentRefresh(int position) {
        super.onFragmentRefresh(position);
        btnGuide.setVisibility(position == 2? View.VISIBLE : View.GONE);
    }


}
